using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIPetListSort : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIPetListSort
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnConfirm { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnCancel { protected set; get; }
            public UnityEngine.GameObject toggleSortType { protected set; get; }
            public UnityEngine.GameObject toggleOrder { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnConfirm", out var __tbv0);
                this.btnConfirm = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnCancel", out var __tbv1);
                this.btnCancel = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleSortType", out var __tbv2);
                this.toggleSortType = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleOrder", out var __tbv3);
                this.toggleOrder = __tbv3;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIPetListSort),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields
        private ListScrollAdapter<UIN_CommonToggleOn> types;
        private ListScrollAdapter<UIN_CommonToggleOn> orders;

        private PetSort sort = PetSort.Default;
        #endregion fields

        #region properties

        protected UIB_UIPetListSort ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIPetListSort();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.types = this.InitListScroll<UIN_CommonToggleOn>(this.ui.toggleSortType, this.OnTypeChanged);
            this.types.InitUnitSelected(UnitSelectType.ByExToggle, this.OnTypeSelected);

            this.orders = this.InitListScroll<UIN_CommonToggleOn>(this.ui.toggleOrder, this.OnOrderChanged);
            this.orders.InitUnitSelected(UnitSelectType.ByExToggle, this.OnOrderSelected);
        }

        protected override void OnShow(UIBaseData data)
        {
            this.SetData(data);
            this.ShowView();
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.btnConfirm.onClick.AddListener(this.OnClickConfirm);
            this.ui.btnCancel.onClick.AddListener(this.OnClickCancel);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds

        private void SetData(object data)
        {
            if (data is PetSort petSort)
            {
                this.sort.Sync(petSort);
            }
        }

        private void ShowView()
        {
            this.ShowTypes();
            this.ShowOrders();
        }

        private void ShowTypes()
        {
            int cnt = (int)PetSortType.MaxLength - 1;
            int index = -1;
            for (var i = PetSortType.None + 1; i < PetSortType.MaxLength; i++)
            {
                int iVal = (int)i;
                if (this.sort.sortType == 0 || this.sort.sortType == iVal)
                {
                    index = iVal - 1;
                    break;
                }
            }
            this.types.RefillCells(cnt, index);
        }

        private void OnTypeChanged(UIN_CommonToggleOn item, int index)
        {
            int type = index + 1;
            string lab = APIUIPet.GetStrSortType(type);
            item.Show(index, lab);
        }

        private void OnTypeSelected(int index, UIN_CommonToggleOn item)
        {
            if (item == null)
            {
                return;
            }
            int type = index + 1;
            this.sort.sortType = type;
        }

        private void ShowOrders()
        {
            int indexOrder = this.sort.isDesc ? 0 : 1;
            this.orders.RefillCells(2, indexOrder);
        }

        private void OnOrderChanged(UIN_CommonToggleOn item, int index)
        {
            string lab = APIUIPet.GetOrderName(index);
            item.Show(index, lab);
        }

        private void OnOrderSelected(int index, UIN_CommonToggleOn item)
        {
            this.sort.isDesc = index == 0;
        }

        private void OnClickConfirm()
        {
            MsgDispatcher.Broadcast(MsgEventType.PetSort, this.sort);
            this.Close();
        }

        private void OnClickCancel()
        {
            this.Close();
        }

        #endregion methonds
    }
}
